package com.haha.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class HandlerActivity extends AppCompatActivity {
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler);
//        handler = new Handler();
//        //3000毫秒，跳转页面
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent = new Intent(HandlerActivity.this, ButtonActivity.class);
//                startActivity(intent);
//            }
//        },3000);

        //主线程处理
        handler= new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        Toast.makeText(HandlerActivity.this,"消息接收：线程通信成功！",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        //使用创建的线程发送消息
        new Thread(()->{
            Message message = new Message();
            message.what=1;
            handler.sendMessage(message);
        }).start();
    }
}